﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmPersona : Form
    {
        [Dependency]
        public IPersonaBL personaBL { get; set; }

        public FrmPersona()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool isExtend;

        public void Extend()
        {
            isExtend = true;
        }

        private void FrmPersona_Load(object sender, EventArgs e)
        {
            cargaData();
        }

        public void cargaData()
        {
            pERSONABindingSource.DataSource = personaBL.GetAllFromOnlyPersona();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            pERSONABindingSource.DataSource = personaBL.GetAllFromPersonaByDescripcion(txtDescripcion.Text);
        }

        private void txtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                pERSONABindingSource.DataSource = personaBL.GetAllFromPersonaByDescripcion(txtDescripcion.Text);
            }
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoPersona frm = new FrmMttoPersona();
            frm.NuevaPersona();
            frm.ShowDialog();
            cargaData();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var persona = pERSONABindingSource.Current as PERSONA;

            if (persona != null)
            {
                FrmMttoPersona frm = new FrmMttoPersona();
                frm._persona = persona;
                frm.EditarPersona();
                frm.ShowDialog();
                cargaData();
            }
            else
            {
                MessageBox.Show("Debe seleccionar una Persona.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        public PERSONA currentPersona { get; set; }

        private void pERSONADataGridView_DoubleClick(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
                currentPersona = pERSONABindingSource.Current as PERSONA;
                this.Close();
            }
        }
    }
}
